<?php
namespace app\index\controller;
use think\captcha\Captcha;
use think\Controller;
use think\Session;
header("content-type:text/html;charset=utf-8");
class Log extends Controller
{
    public function login()
    {
        return view();
    }
    public function register()
    {
        return view();
    }
    //普通登录
    public function chackLogin()
    {   

        $data['menber'] = input('post.member','','htmlspecialchars');
        $data['password'] = md5(input('post.password','','htmlspecialchars'));
        $user = db('menber')->where(['user'=>$data['menber'],'pwd'=>$data['password']])->find();
        $user_id = $user['id'];
        $nickname = $user['nickname'];
        if ($user) {
            session('user',$user['user']);
            session('nickname',$nickname);
            session('id',$user_id);
            $this->success('登录成功',url('user/center'));
        }
    }
    //普通注册
    public function insert()
    {
        $post['user'] = input('post.user','','htmlspecialchars');
        $post['pwd'] = input('post.pwd','','htmlspecialchars');
        $post['repwd'] = input('post.repwd','','htmlspecialchars');
        $post['email'] = input('post.email','','htmlspecialchars');
        $post['code'] = input('post.code','','htmlspecialchars');
        $data = [
            "user"=> $post['user'],
            "pwd"=> md5($post['pwd']),
            "email"=> $post['email']
        ];
        $menber=validate('member');           
        if(!$menber->check($post)){
            $this->error($menber->getError());
        }
        if ($post['repwd']!=$post['pwd']) {
            $this->error('确认密码与输入密码不一致');
            
        }elseif (!captcha_check($post['code'])) {
            $this->error('验证码错误');
        }else {
            $user = db('menber')->insert($data);
        }
        $user_id = "";
        foreach ($user as $key => $value) {
            $user_id = $value['id'];
        }
        if ($user) {
            session('user',$post['user']);
            session('id',$user_id);
            $this->success('注册成功',url('user/center'));
        }
        
    }
    //普通注册验证码
    public function code()
    {
        $config = [
            'codeSet' => '123',
            'length' => 1,
            'useCurve' => false,
            'useNoise' => false
        ];
       $captcha =  new Captcha($config);
       return $captcha->entry();
    }
    //发送手机验证码
    public function curl(){
        $phone=input('get.phone');

        $menber=validate('phone');   
        $post['phone']=$phone;   
            
        if(!$menber->scene('check_phone')->check($post)){
            $this->error($menber->getError());
        }
        
        $codes=mt_rand(1000,9999);
        $config=['expire'=>60*2];
        $session=new Session($config);
        $session::set('codes',$codes);
        session('phone',$phone);

        
        $url = 'http://v.juhe.cn/sms/send?mobile='.$phone.'&tpl_id=103052&tpl_value=%23code%23%3D'.$codes.'&key=646fd6fd9554c4ef907376e5996fb797';
        $res = curl($url);
        $return = json_decode($res);

        return $return;
    }
    //手机号快速注册或者登录
    public function phoneLogin()
    {
        $post['phone'] = input('post.phone','','htmlspecialchars');
        $post['phonecode'] = input('post.phonecode','','htmlspecialchars');
        $data = [
            "phone"=> $post['phone'],
        ];
        if ($post['phonecode'] != session('codes'))
        {
            $this->error('手机验证码错误');
        }
        if ($post['phone'] != session('phone'))
        {
            $this->error('输入的手机号码跟验证的手机号码不一致');
        }
        $user = db('menber')->where('phone',$post['phone'])->select();
        $user_id = "";
        foreach ($user as $key => $value) {
            $user_id = $value['id'];
        }
        if (!$user) {
            db('menber')->insterGetId($data);
            session('phone',$post['phone']);
            session('id',$user_id);
            $this->success('注册成功',url('user/info'));
        }else {
            session('phone',$post['phone']);
            session('id',$user_id);
            $this->success('登录成功',url('user/info'));
        }
    }
    //微信登录

    public function wxLogin(){
        $code = input('get.code');
        $url1 = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx0e1613a503ae7dcb&secret=9c979a919adc5f6110a2849f80385938&code=".$code."&grant_type=authorization_code";
        $res1 = curl($url1);
        $return1 = json_decode($res1, true);
        $refresh = $return1['refresh_token'];

        $url2 = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=wx0e1613a503ae7dcb&grant_type=refresh_token&refresh_token=".$refresh;
        $res2 = curl($url2);
        $return2 = json_decode($res2, true);
        $access = $return2['access_token'];
        $openid = $return2['openid'];
        
        $url3 = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access."&openid=".$openid."&lang=zh_CN";
        $res3 = curl($url3);
        $return3 = json_decode($res3, true);
        $nickname = $return3['nickname'];
        $headimgurl = $return3['headimgurl'];
        $db = db('menber');
        $res = $db -> where('openid',$openid) -> select();
        $data = [
            'openid'=> $openid,
            'nickname'=> $nickname,
            'headimgurl'=> $headimgurl,
        ];
        if (!$res) {
            $db -> insert($data);
            $res = $db -> where('openid',$openid) -> select();
            foreach ($res as $key => $value) {
                session('id',$value['id']);
                session('nickname',$value['nickname']);
            }
            $this->success('成功进入',url('index/index1'));
        }
        else {
            foreach ($res as $key => $value) {
                session('id',$value['id']);
                session('nickname',$value['nickname']);
            }
            $this->success('成功进入',url('index/index1'));
        }

    }
    //微博登录

    public function checkweibo()
    {
        if(isset($_GET['code'])){
            $code=$_GET['code'];
            $access_token=cache('wb_token');
            if(!$access_token){
                $url='https://api.weibo.com/oauth2/access_token?client_id=3002858390&client_secret=dcbf3f03a71d0e86efcfff92b4ee8aca&grant_type=authorization_code&redirect_uri=http://yyw.weiyinstudio.com/&code='.$code;
                $res = curl($url,'.');
                $access_token=json_decode($res,true);
                cache('wb_token',$access_token,3000);
            }
          
            $url2='https://api.weibo.com/2/users/show.json?access_token='.$access_token['access_token'].'&uid='.$access_token['uid'].'';
            $result=curl($url2);
            $session=json_decode($result,true);
            
            session('session',$session);
            $data['weibo_id']=session('session')['id'];
            $id=db('menber')->where($data)->find();
            if(!$id){
                $post=[
                    'weibo_id'=>session('session')['id'],
                    'weibo_img'=>session('session')['profile_image_url'],
                    'nickname'=>session('session')['screen_name'],
                ];
                db('menber')->insert($post);
                $user = db('menber')->where($data)->find();
                session('id',$user['id']);
                session('nickname',$user['nickname']);
                $this->success('成功进入',url('user/center'));
            }else{
                $user = db('menber')->where($data)->find();
                session('id',$user['id']);
                session('nickname',$user['nickname']);
                $this->success('成功进入',url('user/center'));
            }
        }
    }
    public function logout(){
        Session::clear();
        $this->error('退出成功' , url('log/login'));
    }

}